package zlx.controller;

import org.springframework.cloud.client.discovery.DiscoveryClient;
import com.zlx.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.*;
import zlx.service.DeptService;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    DeptService deptService;
    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/queryall")
    public List<Dept> QueryALL(){
       return deptService.queryAll();
    }

    @GetMapping("/queryById/{id}")
    public Dept get(@PathVariable("id")long id){
        return deptService.queryById(id);
    }

    @PostMapping
    public Boolean add(@RequestBody Dept dept) {
        return deptService.addDept(dept);
    }
    @GetMapping("/discovery")
    public Object discovery(){
//获得微服务列表清单
        List<String> list = discoveryClient.getServices();
        System.out.println("client.getServices()==>"+list);
//得到一个具体的微服务！
        List<ServiceInstance> serviceInstanceList =
                discoveryClient.getInstances("springcloud-provider-dept");
        for (ServiceInstance serviceInstance : serviceInstanceList) {
            System.out.println(
                    serviceInstance.getServiceId()+"\t"+
                            serviceInstance.getHost()+"\t"+
                            serviceInstance.getPort()+"\t"+
                            serviceInstance.getUri()
            );
        }
        return this.discoveryClient;
    }

}
